import axios from './axios'
import qs from 'qs'
import {baseUrl} from '@/config/config'
var apiUrl = baseUrl + '/bdc/api/app/school'
// 获取学校列表
export const getSchoolList = (params) => {
    return new Promise((resolve, reject) => {
        axios({
            url: apiUrl,
            method: 'get',
            params
        }).then(res => {
          resolve(res.data)
        })
    })
}

export const getSchool = (schoolId) => {
  return new Promise((resolve, reject) => {
      axios({
          url: apiUrl + '/' + schoolId,
          method: 'get'
      }).then(res => {
        resolve(res.data)
      })
  })
}

export const putSchool = (data) => {
  var schoolId = data.schoolId
  delete data.schoolId
  return new Promise((resolve, reject) => {
      axios({
          url: apiUrl + '/' + schoolId,
          method: 'put',
          data
      }).then(res => {
        resolve(res.data)
      })
  })
}

export const addSchool = (data) => {
  return new Promise((resolve, reject) => {
      axios({
          url: apiUrl,
          method: 'post',
          data
      }).then(res => {
        resolve(res)
      })
  })
}
// 根据id获取学校
export const getSchoolById = (id) => {
  return new Promise((resolve, reject) => {
      axios({
          url: apiUrl + '/' + id,
          method: 'get'
      }).then(res => {
        resolve(res)
      })
  })
}
// 更新学校信息
export const updateSchool = (data) => {
  return new Promise((resolve, reject) => {
      axios({
          url: apiUrl + '/' + data.id + '/detail',
          method: 'put',
          data
      }).then(res => {
        resolve(res)
      }).catch(res => {
        reject(res)
      })
  })
}
// 删除学校
export const deleteSchool = (id) => {
  return new Promise((resolve, reject) => {
      axios({
          url: apiUrl + '/' + id,
          method: 'delete'
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取省份
export const provinces = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/areas/provinces',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取区域
export const areas = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/areas',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取学段列表
export const stage = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/stages',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取学校的学段
export const getSchoolStage = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/' + params + '/stages',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取学科列表  /bdc/api/app/school/{id}/subjects
export const subjectsBySchoolId = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/subjects',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取年级列表
export const gradesBySchoolId = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/grades',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取年级列表
export const getGradeList = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/' + params.schoolId + '/grades',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取该学校的所有班级
export const getClassList = (params) => {
  return new Promise((resolve, reject) => {
    axios({
      url: baseUrl + '/bdc/api/app/school/' + params.schoolId + '/classes',
      method: 'get',
      params
    }).then(res => {
      resolve(res.data)
    })
  })
}
// 字典数据
export const dictionaryvalue = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/dictionaryvalue',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 创建学校
export const creatSchool = (data) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/detail',
          method: 'post',
          data
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 根据id获取学校详情
export const schoolDetail = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/' + params.id + '/detail',
          method: 'get'
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取学校代理商
export const agents = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/agents',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 获取学年 /bdc/api/app/school/{id}/years
export const schoolYear = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/years',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 导出学校列表
export const exportSchoolList = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/exportSchoolList',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      })
  })
}

export const importTerm = (data) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/school/' + data.schoolId + '/terms',
          method: 'post',
          data: data.body
      }).then(res => {
        resolve(res.data)
      })
  })
}
// 通过userName获取用户信息/bdc/api/app/users/user
export const userInfo = (params) => {
  return new Promise((resolve, reject) => {
      axios({
          url: baseUrl + '/bdc/api/app/users/user',
          method: 'get',
          params
      }).then(res => {
        resolve(res.data)
      }).catch(res => {
        reject(res)
      })
  })
}

export const getPeroidsList = (params) => {
  return new Promise((resolve, reject) => {
      if (!params.schoolId) {
        resolve([])
        return
      }
      axios({
        url: baseUrl + '/bdc/api/app/school/' + params.schoolId + '/peroids',
        method: 'get',
        params
      }).then(res => {
        resolve(res.data.items)
      }).catch(res => {
        reject(res)
      })
  })
}
